package com.yuhanginfo.modules.web.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yuhanginfo.common.security.Digests;
import com.yuhanginfo.common.service.WebCrudService;
import com.yuhanginfo.common.utils.Encodes;
import com.yuhanginfo.modules.web.dao.WebStoreDao;
import com.yuhanginfo.modules.web.dao.WebYongUserDao;
import com.yuhanginfo.modules.web.entity.WebStore;
import com.yuhanginfo.modules.web.entity.WebYongUser;
/**
 * 门店管理Service
 * @author yangs
 * @version 2017-09-13
 */
@Service
@Transactional(readOnly = true)
public class WebYongUserService extends WebCrudService<WebYongUserDao, WebYongUser> {
    @Autowired
    private WebYongUserDao webYongUserDao;
    @Autowired
    private WebStoreDao storeDao;
	
    public static final String HASH_ALGORITHM = "SHA-1";
	public static final int HASH_INTERATIONS = 1024;
	public static final int SALT_SIZE = 8;
	
	public WebYongUser get(String id) {
		return super.get(id);
	}
	
	public boolean existUserByLoginName(WebYongUser user) {
		WebYongUser result = webYongUserDao.getByLoginName(user);
		return result!=null;
	}
	
	public boolean existUserByEmail(WebYongUser user) {
		WebYongUser result = webYongUserDao.getByEmail(user);
		return result!=null;
	}
	
	public boolean existUserByMobile(WebYongUser user) {
		WebYongUser result = webYongUserDao.getByMobile(user);
		return result!=null;
	}
	
	public boolean existUserByLinkmanMobile(WebYongUser user) {
		WebYongUser result = webYongUserDao.getByMobile(user);
		return result!=null;
	}
	
	public boolean existLinkmanEmail(WebYongUser user) {
		WebYongUser result = webYongUserDao.getByLinkmanEmail(user);
		return result!=null;
	}
	
	public boolean existLinkmanPhone(WebYongUser user) {
		WebYongUser result = webYongUserDao.getByLinkmanPhone(user);
		return result!=null;
	}
	
	@Transactional(readOnly = false)
	public void saveUser(WebYongUser user) {
			Date now = new Date();
			user.preInsert();
			user.setUserType("1");
			user.setExamineState("0");
			user.setCreateDate(now);
			user.setUpdateDate(now);
			user.setPassword(entryptPassword(user.getPassword()));
			webYongUserDao.insert(user);
	}
	
	public static String entryptPassword(String plainPassword) {
		String plain = Encodes.unescapeHtml(plainPassword);
		byte[] salt = Digests.generateSalt(SALT_SIZE);
		byte[] hashPassword = Digests.sha1(plain.getBytes(), salt, HASH_INTERATIONS);
		return Encodes.encodeHex(salt)+Encodes.encodeHex(hashPassword);
	}
	
	/**
	 * 验证密码
	 * @param plainPassword 明文密码
	 * @param password 密文密码
	 * @return 验证成功返回true
	 */
	public boolean validatePassword(String plainPassword, String password) {
		String plain = Encodes.unescapeHtml(plainPassword);
		byte[] salt = Encodes.decodeHex(password.substring(0,16));
		byte[] hashPassword = Digests.sha1(plain.getBytes(), salt, HASH_INTERATIONS);
		return password.equals(Encodes.encodeHex(salt)+Encodes.encodeHex(hashPassword));
	}

	
	@Transactional(readOnly = false)
	public void updatePasswordByMobile(WebYongUser user) {
			user.setPassword(entryptPassword(user.getPassword()));
			webYongUserDao.updatePasswordByMobile(user);
	}

	
	@Transactional(readOnly = false)
	public void updateLinkMan(WebYongUser user) {
		WebYongUser webYongUser=webYongUserDao.get(user.getId());
		webYongUser.setLinkman(user.getLinkman());
		webYongUser.setEmail(user.getEmail());
		webYongUser.setLinkmanPhone(user.getLinkmanPhone());
		webYongUserDao.update(webYongUser);
	}
	
	public List<WebStore> getStoreByUserId(String userId){
		return storeDao.findStoreByUserId(userId);
	}

}